package com.kyzh.core.dialog

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.VideoDiscuss
import com.gushenge.core.requests.VideoRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemVideoDiscussBinding
import com.kyzh.core.utils.onLoginClick
import com.gushenge.core.toast


private var dialog:AlertDialog?=null

fun Context.showVideoDiscussDialog(id: String){
    dialog = AlertDialog.Builder(this,R.style.videodiscussdialog)
            .setView(getView(id))
            .create()
    val window = dialog?.window
    window?.setGravity(Gravity.BOTTOM)

    window?.decorView?.setPadding(0, 0, 0, 0)
    val lp: WindowManager.LayoutParams? = window?.attributes
    lp?.width = WindowManager.LayoutParams.FILL_PARENT
    lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
    window?.attributes = lp

    dialog?.show()
}

private fun Context.getView(id: String): View {
    val view = View.inflate(this, R.layout.dialog_video_discuss, null)

    view.findViewById<ImageView>(R.id.ivClose).setOnClickListener { dialog?.dismiss() }
    val rvDiscuss = view.findViewById<RecyclerView>(R.id.rvDiscuss)
    val rvDiscussAdd = view.findViewById<ImageView>(R.id.ivAddDiscuss)
    val beans = arrayListOf<VideoDiscuss>()
    val  adapter = DiscussAdapter(R.layout.item_video_discuss, beans)
    rvDiscuss.adapter = adapter
    VideoRequest.videoDiscuss(id){
        beans.clear()
        beans.addAll(this)
        adapter.notifyDataSetChanged()
    }
    adapter.setOnItemChildClickListener { adapter, view, position ->
        when(view.id){
            R.id.tvZan -> {
                val bean = beans[position]
                VideoRequest.videoDiscussZan(bean.id){
                    beans[position].zan = if (bean.is_zan) bean.zan-1 else bean.zan +1
                    beans[position].is_zan=!bean.is_zan
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }

    rvDiscussAdd.onLoginClick(this) {
        showVideoDiscussAddDialog(id,true){
            getString(R.string.discussCommitSuccess).toast()
        }
    }
    return view
}


private class DiscussAdapter(layout:Int,beans:ArrayList<VideoDiscuss>):BaseQuickAdapter<VideoDiscuss,BaseViewHolder>(layout,beans),LoadMoreModule{

    init {
        addChildClickViewIds(R.id.tvZan)
    }
    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        DataBindingUtil.bind<ItemVideoDiscussBinding>(viewHolder.itemView)
    }
    override fun convert(holder: BaseViewHolder, item: VideoDiscuss) {
        holder.getBinding<ItemVideoDiscussBinding>()?.data = item
    }
}